package cn.com.pxto.web.tag;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

import cn.com.pxto.dao.SchoolDAO;

public class SchoolNameTag extends BodyTagSupport {
	private String sid;

	private static  SchoolDAO schoolDAO;
	
	private static HashMap<String , String> schoolNameMap;
	
	
	
	public SchoolNameTag() {
				
	}

	public int doStartTag() throws JspException {
		Boolean schoolHasNew=(Boolean) pageContext.getServletContext().getAttribute("schoolHasNew");
		if(schoolDAO == null ){
			// ��ϵͳ�л�ȡspring����
			ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
			schoolDAO = (SchoolDAO) ctx.getBean("schoolDAO");
			schoolNameMap = schoolDAO.listSchoolForTag();
		}else{
			if(schoolHasNew!= null){
				ApplicationContext ctx = (XmlWebApplicationContext) pageContext.getServletContext().getAttribute("org.springframework.web.struts.ContextLoaderPlugIn.CONTEXT.");
				schoolDAO = (SchoolDAO) ctx.getBean("schoolDAO");
				schoolNameMap = schoolDAO.listSchoolForTag();
				pageContext.getServletContext().removeAttribute("schoolHasNew");
			}
		}
		String schoolName = schoolNameMap.get(sid);
		try {
			if (schoolName == null) {
				pageContext.getOut().print("-1");
			} else {
				pageContext.getOut().print(schoolName);
			}
		} catch (IOException e) {
			throw new JspException("Print Error:" + e.getMessage());
		}
		return super.doStartTag();
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

}
